@model BlogContentModel

@{
    //page title
    ViewBag.PageTitle = T("Admin.ContentManagement.Blog.BlogPosts").Text;
    //active menu item (system name)
    NopHtml.SetActiveMenuItemSystemName("Blog posts");
}

@{
    const string hideSearchBlockAttributeName = "BlogPostsPage.HideSearchBlock";
    var hideSearchBlock = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideSearchBlockAttributeName);
}


<div class="content-header clearfix">
    <h1 class="float-left">
        @T("Admin.ContentManagement.Blog.BlogPosts")
    </h1>
    <div class="float-right">
        <a asp-action="BlogPostCreate" class="btn btn-primary">
            <i class="fas fa-plus-square"></i>
            @T("Admin.Common.AddNew")
        </a>
        @await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.BlogListButtons, additionalData = Model })
    </div>
</div>

<section class="content">
    <div class="container-fluid">
        <div class="form-horizontal">
            <div class="form-horizontal">
                <div class="cards-group">

                    @*hide the entire search block if no elements are displayed*@
                    <div class="card card-default card-search">
                        <div class="card-body">
                            <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                                <div class="search-text">@T("Admin.Common.Search")</div>
                                <div class="icon-search"><i class="fas fa-search" aria-hidden="true"></i></div>
                                <div class="icon-collapse"><i class="far fa-angle-@(!hideSearchBlock ? "up" : "down")" aria-hidden="true"></i></div>
                            </div>

                            <div class="search-body @(hideSearchBlock ? "closed" : "")">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group row" @(Model.BlogPosts.HideStoresList ? Html.Raw("style=\"display:none\"") : null)>
                                            <div class="col-md-4">
                                                <nop-label asp-for="BlogPosts.SearchStoreId" />
                                            </div>
                                            <div class="col-md-8">
                                                <nop-select asp-for="BlogPosts.SearchStoreId" asp-items="Model.BlogPosts.AvailableStores" />
                                            </div>
                                        </div>
											<div class="form-group row">
											<div class="col-md-4">
												<nop-label asp-for="SearchTitle" />
											</div>
											<div class="col-md-8">
												<nop-editor asp-for="SearchTitle" />
											</div>
										</div>                                        
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-8 offset-md-4">
                                        <button type="button" id="search-blogpost" class="btn btn-primary btn-search">
                                            <i class="fas fa-search"></i>
                                            @T("Admin.Common.Search")
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card card-default">
                        <div class="card-body">
                            <nop-doc-reference asp-string-resource="@T("Admin.Documentation.Reference.Blog", Docs.Blog + Utm.OnAdmin)" />

                            @await Html.PartialAsync("Table", new DataTablesModel
                            {
                                Name = "blogpost-grid",
                                UrlRead = new DataUrl("List", "Blog", null),
                                SearchButtonId = "search-blogpost",
                                Length = Model.BlogPosts.PageSize,
                                LengthMenu = Model.BlogPosts.AvailablePageSizes,
                                Filters = new List<FilterParameter>
                                {
                                    new FilterParameter(nameof(Model.BlogPosts.SearchStoreId), nameof(Model.BlogPosts)),
                                    new FilterParameter(nameof(Model.SearchTitle))
                                },
                                ColumnCollection = new List<ColumnProperty>
                                {
                                    new ColumnProperty(nameof(BlogPostModel.Title))
                                    {
                                        Title = T("Admin.ContentManagement.Blog.BlogPosts.Fields.Title").Text,
                                        Width = "400"
                                    },
                                    new ColumnProperty(nameof(BlogPostModel.LanguageName))
                                    {
                                        Title = T("Admin.ContentManagement.Blog.BlogPosts.Fields.Language").Text,
                                        Width = "150"
                                    },
                                    new ColumnProperty(nameof(BlogPostModel.Id))
                                    {
                                        Title = T("Admin.ContentManagement.Blog.BlogPosts.Fields.Comments").Text,
                                        Width = "200",
                                        ClassName = NopColumnClassDefaults.Button,
                                        Render = new RenderCustom("renderColumnComments")
                                    },
                                    new ColumnProperty(nameof(BlogPostModel.StartDateUtc))
                                    {
                                        Title = T("Admin.ContentManagement.Blog.BlogPosts.Fields.StartDate").Text,
                                        Width = "200",
                                        Render = new RenderDate()
                                    },
                                    new ColumnProperty(nameof(BlogPostModel.EndDateUtc))
                                    {
                                        Title = T("Admin.ContentManagement.Blog.BlogPosts.Fields.EndDate").Text,
                                        Width = "200",
                                        Render = new RenderDate()
                                    },
                                    new ColumnProperty(nameof(BlogPostModel.CreatedOn))
                                    {
                                        Title = T("Admin.ContentManagement.Blog.BlogPosts.Fields.CreatedOn").Text,
                                        Width = "200",
                                        Render = new RenderDate()
                                    },
                                    new ColumnProperty(nameof(BlogPostModel.Id))
                                    {
                                        Title = T("Admin.Common.Edit").Text,
                                        Width = "100",
                                        ClassName = NopColumnClassDefaults.Button,
                                        Render = new RenderButtonEdit(new DataUrl("BlogPostEdit"))
                                    }
                                }
                            })

                            <script>
                        function renderColumnComments(data, type, row, meta) {
                            var link = "BlogComments?filterByBlogPostId=" + data;
                            var title = '@T("Admin.ContentManagement.Blog.BlogPosts.Fields.Comments").Text';
                            var countComments = row.ApprovedComments + row.NotApprovedComments;
                            return '<a class="btn btn-default" href="' + link + '">' + title + ' - ' + countComments + ' (' + row.NotApprovedComments + ')' + '</a>';
                            }
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>